<template>
  <el-form :model="formData" :rules="formRules" size="small" :inline="false" label-width="100px">
    <el-form-item label="事项主题" prop="title">
      <el-input v-model="formData.title" placeholder="请填写事项主题" maxlength="32" />
    </el-form-item>
    <el-form-item label="目标" prop="targetContent">
      <el-select v-model="formData.targetType" style="width: 120px">
        <el-option label="团队目标" value="0" />
        <el-option label="个人目标" value="1" />
        <el-option label="公司目标" value="2" />
      </el-select>
      <el-input v-model="formData.targetContent" placeholder="请填写目标" style="width: 300px" clearable/>
    </el-form-item>
    <el-form-item label="目标描述" prop="content">
      <editor v-model="formData.content" height="160px"/>
    </el-form-item>
    <el-form-item label="行动时间">
      <div style="max-width: 400px">
        <el-date-picker
            v-if="dateType"
            v-model="dateTime"
            style="width: 100%"
            type="datetimerange"
            range-separator="至"
            start-placeholder="行动开始时间"
            end-placeholder="行动结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="selectTime"
        />
        <el-date-picker
            v-else
            v-model="formData.actionStartTime"
            type="datetime"
            style="width: 100%"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择行动时间"
        />
      </div>
      <el-button style="margin-left: 10px" plain :icon="Refresh" @click="dateType = !dateType"/>
    </el-form-item>
    <el-form-item label="执行人" prop="executorIds">
      <el-select v-model="formData.executorIds" placeholder="请选择执行人" size="small" multiple filterable @change="executorChange">
        <el-option v-for="item in executorList" :key="item.userId" :value="item.userId" :label="item.nickname"/>
      </el-select>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="formData.remark" placeholder="请填写备注" type="textarea" :rows="3"/>
    </el-form-item>
    <el-form-item label="优先度">
      <el-radio-group v-model="formData.priority" size="small">
        <el-radio :label="0" size="small">普通</el-radio>
        <el-radio :label="1" size="small">高</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="通知时间" prop="remindState">
      <el-radio-group v-model="formData.remindState">
        <el-radio v-for="item in remindTimes" :key="item.value" :label="item.value" size="small">{{ item.label }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="通知方式" prop="remindType">
      <el-checkbox-group v-model="formData.remindType" size="small">
        <el-checkbox label="1">应用、公众号通知</el-checkbox>
        <el-checkbox label="2">短信通知</el-checkbox>
      </el-checkbox-group>
    </el-form-item>
  </el-form>
</template>

<script>
// 目标事项

import {onMounted, reactive, toRefs} from 'vue'
import {getEnterpriseUsers} from '/@/api/sys/user'
import {ElMessage} from 'element-plus'
import Editor from '/@/components/editor/index.vue'
import {getRemindTimes} from '/@/api/matter'
import Cookies from 'js-cookie'
import {Refresh} from '@element-plus/icons-vue'
import {formatDate} from '/@/utils/formatTime'

export default {
  name: 'Purpose',
  components: {
    Editor
  },
  setup() {
    const state = reactive({
      formData: {
        title: '目标',
        content: '',
        executorIds: [],
        targetType: '0',
        targetContent: null,
        actionStartTime: null,
        actionEndTime: null,
        remark: null,
        priority: 0,
        remindState: 'NONE',
        type: 'OBJECTIVE'   // 事项类型
      },
      formRules: {
        title: {required: true, message: '请填写事项主题', trigger: 'blur'},
        targetContent: {required: true, message: '请填写目标', trigger: 'blur'}
      },
      dateType: false,  // 行动时间范围/单体切换
      dateTime: [],
      remindTimes: [],
      executorList: [],
      selfId: null
    })

    const executorChange = (val) => {
      if (!val.includes(state.selfId)) {
        state.formData.executorIds.push(state.selfId)
        ElMessage.warning('本人必须在执行人中，无法移除')
      }
    }
    const selectTime = (val) => {
      if (val && val.length === 2) {
        state.formData.actionStartTime = val[0]
        state.formData.actionEndTime = val[1]
      } else {
        state.formData.actionStartTime = state.formData.actionEndTime = null
      }
    }

    onMounted(() => {
      // 默认带入自己
      state.selfId = Cookies.get('userId')
      state.formData.executorIds.push(state.selfId)
      // 行动时间默认现在
      let nowTime = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS')
      state.formData.actionStartTime = nowTime
      state.dateTime = [nowTime, nowTime]
      getEnterpriseUsers().then(res => state.executorList = res.data)
      getRemindTimes().then(res => state.remindTimes = res.data)
    })

    return {
      ...toRefs(state),
      executorChange,
      selectTime,
      Refresh
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
